/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * newcliente.java
 *
 * Created on 30/Dez/2011, 15:39:26
 */

package vetclini;

import java.util.ArrayList;
import javax.swing.JFrame;
import org.jdesktop.application.Action;
import veterinario.Animal;
import veterinario.Cliente;
import veterinario.Criacao;
import veterinario.Estimacao;

/**
 *
 * @author 2011101993 - Hugo Amaro
 * @author 2006130415 - Jorge Martins Alves
 */
public class newcliente extends javax.swing.JPanel {

    private VetCliniView pai;
    private JFrame minhaframe;
    private ArrayList<Animal> bixos = new ArrayList<Animal>();
    
    /** Creates new form newcliente */
    public newcliente(JFrame fra) {
        initComponents();
        this.minhaframe = fra;
    }

    public void setPai(VetCliniView parent){
        this.pai = parent;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        concluir = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        nome = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        listaAnimais = new javax.swing.JList();
        jLabel3 = new javax.swing.JLabel();
        novoAnimal = new javax.swing.JButton();
        apagarAnimal = new javax.swing.JButton();

        setName("Form"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(vetclini.VetCliniApp.class).getContext().getActionMap(newcliente.class, this);
        concluir.setAction(actionMap.get("clickedConcluir")); // NOI18N
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(vetclini.VetCliniApp.class).getContext().getResourceMap(newcliente.class);
        concluir.setText(resourceMap.getString("butaoConcluir.text")); // NOI18N
        concluir.setName("butaoConcluir"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        nome.setName("nome"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        listaAnimais.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        listaAnimais.setName("animais"); // NOI18N
        jScrollPane1.setViewportView(listaAnimais);

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        novoAnimal.setAction(actionMap.get("clickedNovoAnimal")); // NOI18N
        novoAnimal.setFont(resourceMap.getFont("novoAnimalCliente.font")); // NOI18N
        novoAnimal.setForeground(resourceMap.getColor("novoAnimalCliente.foreground")); // NOI18N
        novoAnimal.setText(resourceMap.getString("novoAnimalCliente.text")); // NOI18N
        novoAnimal.setName("novoAnimalCliente"); // NOI18N

        apagarAnimal.setAction(actionMap.get("clickedTirarAnimal")); // NOI18N
        apagarAnimal.setFont(resourceMap.getFont("apagarAnimalCliente.font")); // NOI18N
        apagarAnimal.setForeground(resourceMap.getColor("apagarAnimalCliente.foreground")); // NOI18N
        apagarAnimal.setText(resourceMap.getString("apagarAnimalCliente.text")); // NOI18N
        apagarAnimal.setName("apagarAnimalCliente"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(19, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(novoAnimal, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(apagarAnimal))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 276, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel3)
                                .addComponent(jLabel2))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(nome, javax.swing.GroupLayout.PREFERRED_SIZE, 223, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(159, Short.MAX_VALUE)
                .addComponent(concluir)
                .addGap(75, 75, 75))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addContainerGap(230, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(nome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(novoAnimal)
                    .addComponent(apagarAnimal))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(concluir)
                .addGap(50, 50, 50))
        );
    }// </editor-fold>//GEN-END:initComponents

    @Action
    public void clickedNovoAnimal() {
        JFrame novaJanela = new JFrame("Novo Animal");
        newbixo newContentPane = new newbixo(novaJanela);
        newContentPane.setPai(this);
         newContentPane.setOpaque(true);
         novaJanela.setContentPane(newContentPane);

         novaJanela.pack();
         novaJanela.setVisible(true);
         novaJanela.requestFocus();
    }

    public void addAnimal(Animal novo){
        this.bixos.add(novo);
    }

    public void updateList(){
        String[] listaNova = new String[bixos.size()];
        for(int i=0; i<bixos.size(); i++){
            if(bixos.get(i).getClass()==Criacao.class)
                listaNova[i] = "Raça: "+((Criacao)bixos.get(i)).getRaca()+" Tipo: Criação";
            else
                listaNova[i] = "Raça: "+((Estimacao)bixos.get(i)).getRaca()+" Tipo: Estimação";
        }
        listaAnimais.setListData(listaNova);
    }

    @Action
    public void clickedConcluir() {
        if (nome.getText().length()>0){
            Cliente novoCliente = new Cliente(nome.getText(), this.bixos);
            pai.getClinica().addCliente(novoCliente);
            pai.updateLists();
            this.minhaframe.dispose();
        }else{
         janelaErro janelaErro = new janelaErro("Deve preencher o campo Nome");
         janelaErro.pack();
         janelaErro.setVisible(true);
         janelaErro.requestFocus();
        }
    }

    @Action
    public void clickedTirarAnimal() {
        if(listaAnimais.getSelectedIndex()>=0)
            this.bixos.remove(listaAnimais.getSelectedIndex());
        updateList();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton apagarAnimal;
    private javax.swing.JButton concluir;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList listaAnimais;
    private javax.swing.JTextField nome;
    private javax.swing.JButton novoAnimal;
    // End of variables declaration//GEN-END:variables

}
